<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head th:include="css :: css">
</head>
<link href="/js/fileUpload/fileinput.css" rel="stylesheet">
<body style="opacity: 1">
<div style="padding-top: 0">
    <div class="container-fluid">
        <div class="cl-mcont">
            <div class="row">
                <div class="col-md-12" style="padding:0">
                    <div class="block-flat">
                        <form class="form-horizontal group-border-dashed" id="buyCode">
                            <input type="hidden" id="productTypes" name="productTypes">
                            <input type="hidden" id="statusArray" name="statusArray">
                            <div class="form-group">
                                <div class="input-group">
                                    <input type="text" class="form-control" id="spuId_query" name="spuId"
                                           placeholder="SPU编号"/>
                                </div>
                                <div class="input-group">
                                    <input type="text" class="form-control" id="spuName_query"
                                           name="productName"
                                           placeholder="SPU产品名称"/>
                                </div>
                                <div class="input-group">
                                    <select class="selectpicker" title="运营商"
                                            id="productType"
                                            name="productType"
                                            data-live-search="true">
                                    </select>
                                </div>

                                <div class="input-group">
                                    <select class="form-control"
                                            id="spuStatus_query"
                                            name="spuStatus">
                                        <option value="">SPU状态</option>
                                        <option value="0">上架</option>
                                        <option value="1">下架</option>
                                    </select>
                                </div>
                                <div class="input-group">
                                    <select class="form-control"
                                            id="aliProductStatus_query"
                                            name="aliProductStatus">
                                        <option value="">小程序状态</option>
                                        <option value="ON">上架</option>
                                        <option value="OFF">下架</option>
                                    </select>
                                </div>
                                <div class="input-group">
                                    <select class="form-control"
                                            id="submitType_query"
                                            name="submitType">
                                        <option value="">卡类型</option>
                                        <option value="0">上传图片</option>
                                        <option value="1">卡密</option>
                                        <option value="2">二维码+链接</option>
                                    </select>
                                </div>
                                <div class="input-group">
                                    <button class="btn btn-primary" type="button" id="search"><i
                                            class="fa fa-search"></i>查询
                                    </button>
                                </div>
                                <div class="input-group">
                                    <button class="btn btn-success" type="button" id="success"
                                            onclick="updateStatus('ON')"><i class="fa fa-reply-all"></i>上架
                                    </button>&nbsp;
                                </div>
                                <div class="input-group">
                                    <button class="btn btn-danger" type="button" id="fail"
                                            onclick="updateStatus('OFF')"><i
                                            class="fa fa-lock"></i>下架
                                    </button>&nbsp;
                                </div>
                                <div class="input-group">
                                    <button class="btn btn-success" type="button" id="Add_button"><i
                                            class="fa fa-check-circle"></i>新增
                                    </button>
                                </div>
                                <div class="input-group">
                                    <button class="btn btn-warning" type="button" id="Update_button"><i
                                            class="fa fa-pencil"></i>修改
                                    </button>
                                </div>
                                <div class="input-group">
                                    <button class="btn btn-danger" type="button" id="Delete_button"><i
                                            class="fa fa-trash-o"></i>删除
                                    </button>
                                </div>
                            </div>
                        </form>
                    </div>
                    <div class="block-flat">
                        <div class="content">
                            <div class="table-responsive">
                                <table class="table hover" id="dataTable">
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <!--新增 -->
    <div class="modal fade" id="addModel" tabindex="-1"
         role="dialog"
         aria-labelledby="updateUserModalLabel">
        <div class="modal-dialog" role="document" style="width:800px;">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal"
                            aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="updateUserModalLabel">新增图片</h4>
                </div>
                <div class="modal-body" style="min-height:100px;">
                    <form class="form-horizontal" enctype="multipart/form-data"
                          method="post"
                          action="/aliProgramXianyuProduct/addAliProgramXianyuProduct"
                          id="addForm" name="addForm">
                        <input type="hidden" id="JSESSIONID" name="JSESSIONID">
                        <input type="hidden" id="imageString" name="imageString">
                        <div class="form-group">
                            <label class="col-sm-2 control-label">运营商</label>
                            <div class="col-sm-10" id="productType_add_div">
                                <select class="selectpicker" title="运营商"
                                        id="productType_add"
                                        name="productType"
                                        data-live-search="true">
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">SPU产品名称</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" placeholder="SPU产品名称" id="spuName_add"
                                       name="spuName">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">SPU编号</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" placeholder="SPU编号" id="spuId_add"
                                       name="spuId">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">图片</label>
                            <div class="col-sm-10">
                                <input type="file" name="picFile" id="choseFile" style="display: none"
                                       onchange="preview(this)"/>
                                <img class="chose-commodity-face" id="img_add" onclick="toChoseFile(this)"
                                     th:attr="src=@{/images/no-logo.png}"/>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default"
                            data-dismiss="modal">取消
                    </button>
                    <button type="button" class="btn btn-primary" id="addSave">确定</button>
                    </button>
                </div>
            </div>
        </div>
    </div>
    <!-- 修改 -->
    <div class="modal fade" id="updateModel" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel2">修改图片</h4>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal"
                          enctype="multipart/form-data"
                          method="post"
                          action="/aliProgramXianyuProduct/updateAliProgramXianyuProduct"
                          id="updateForm" name="updateForm">
                        <input type="hidden" id="JSESSIONID_UPDATE" name="JSESSIONID">
                        <input type="hidden" id="aliKey_update" name="aliKey">
                        <input name="id" id="id_update" type="hidden"/>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">运营商</label>
                            <div class="col-sm-10" id="productType_update_div">
                                <select class="selectpicker" title="运营商"
                                        id="productType_update"
                                        name="productType"
                                        data-live-search="true">
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">SPU产品名称</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" placeholder="SPU产品名称" id="spuName_update"
                                       name="spuName">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">SPU编号</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" placeholder="SPU编号" id="spuId_update"
                                       name="spuId">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">图片</label>
                            <div class="col-sm-10">
                                <input type="file" name="picFile" id="choseFile2" style="display: none"
                                       onchange="preview(this)"/>
                                <img class="chose-commodity-face" id="file2" onclick="toChoseFileForUpdate(this)"
                                     th:attr="src=@{/images/no-logo.png}"/>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" id="updateSave">确定</button>
                </div>
            </div>
        </div>
    </div>
    <!--产品状态-->
    <div class="modal fade" id="productModal" tabindex="-1" role="dialog" aria-labelledby="updateStatusLabel">
        <div class="modal-dialog" role="document" style="width:800px;">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="updateStatusLabel">状态更新</h4>
                </div>
                <div class="modal-body" style="min-height: 100px;">
                    <form class="form-horizontal" role="form" id="maintainForm">
                        <input type="hidden" id="ids" name="ids">
                        <input type="hidden" id="aliProductStatus" name="aliProductStatus">
                        <div class="col-sm-6">
                            <div class="form-group">
                                <label for="remark" class="col-sm-4 control-label"><span
                                        class="text-danger"></span>操作状态:</label>
                                <div class="col-sm-7" style="width: 240px">
                                    <input type="text" class="form-control"
                                           id="aliProductStatusStr" name="aliProductStatusStr" value="disabled"
                                           disabled>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="remark" class="col-sm-4 control-label"><span
                                        class="text-danger"></span>备注:</label>
                                <div class="col-sm-7" style="width: 240px">
                                    <input type="text" class="form-control"
                                           id="remark" name="remark">
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" id="toSaveRemark">确定</button>
                </div>
            </div>
        </div>
    </div>
</div>
<div th:include="js :: js"></div>
<script type="text/javascript" th:attr="src=@{/js/jquery.form.js}"></script>
<script type="text/javascript" th:attr="src=@{/js/jquery.parser.js}"></script>
<script type="text/javascript" src="js/jquery.icheck/icheck.min.js"></script>
<script type="text/javascript" src="js/fileUpload/fileinput.min.js"></script>
<script type="text/javascript" src="js/fileUpload/zh.js"></script>
<script>
    $(function () {
        var addForm = $("#addForm").validate({
            rules: {
                spuName: {
                    required: true
                },
                spuId: {
                    required: true
                }
            }
        });
        var updateForm = $("#updateForm").validate({
            rules: {
                spuName: {
                    required: true
                },
                spuId: {
                    required: true
                }
            }
        });
        $.post("/CardProductController/queryAllCardOperatorList", function (data) {
            $("#productType").html("");
            var str = "<option value=\"\">全部</option>";
            if (data != null && data.length > 0) {
                for (var i = 0; i < data.length; i++) {
                    str += '<option value=' + data[i].cardOperatorId + '>' + data[i].cardOperatorName + '</option>'
                }
            }
            $("#productType").append(str);
            $("#productType_add").append(str);
            $("#productType_update").append(str);
            //缺一不可  
            $('.selectpicker').selectpicker('refresh');
            $('.selectpicker').selectpicker('render');
        }).error(function (e) {
            layer.msg(e);
        });

        $('#dataTable').bootstrapTable({
            url: '/aliProgramXianyuProduct/queryPage',
            height: $(window).height() - 200,
            pageSize: 100,
            pagination: true,
            singleSelect: true,
            pageList: [30, 50, 100],
            clickToSelect: true,
            queryParams: queryParams,
            uniqueId: 'id',
            queryParamsType: '',
            sidePagination: 'server',
            responseHandler: function (res) {
                var list = res.rows;
                for (var i = 0; i < list.length; i++) {
                    for (var item in list[i]) {
                        if (list[i][item] == null) {
                            list[i][item] = '-';
                        }
                    }
                }
                res.rows = list;
                return res;
            },
            columns: [
                {
                    checkbox: true,
                    align: 'center'
                },
                {
                    field: 'cardOperatorName',
                    title: '运营商',
                    width: 90, align: 'center'
                }, {
                    field: 'spuName',
                    title: 'SPU产品名称',
                    width: 90, align: 'center'
                },
                {
                    field: 'spuId',
                    title: '闲鱼SPU',
                    width: 90, align: 'center'
                }, {
                    field: 'picUrl',
                    title: '示例图片',
                    width: 90, align: 'center', formatter: function (v, row, index) {
                        var str = "<div class='avatar' >" +
                            "<img style='width:80px' data-id='" + row.id + "' data-field='"
                            + row.picUrl
                            + "' onclick=watchDetailById(this,'" + row.id + "') src='"
                            + row.picUrl + "' onerror=\"this.src='/images/default.png'\"/>";
                        return str;
                    }
                }, {
                    field: 'submitType',
                    title: '卡类型',
                    width: 50,
                    align: 'center',
                    formatter: function (v, row, index) {
                        return submitType(v);
                    }
                }, {
                    field: 'spuStatus',
                    title: 'SPU状态',
                    width: 50,
                    align: 'center',
                    formatter: function (v, row, index) {
                        return spuStatus(v);
                    }
                },
                {
                    field: 'aliProductStatus',
                    title: '小程序状态',
                    width: 50,
                    align: 'center',
                    formatter: function (v, row, index) {
                        return productStatus(v);
                    }
                }, {
                    field: 'createTime',
                    title: '创建时间',
                    width: 50,
                    align: 'center'
                }, {
                    field: 'updateTime',
                    title: '修改时间',
                    width: 50,
                    align: 'center'
                }, {
                    field: 'remark',
                    title: '备注',
                    width: 50,
                    align: 'center'
                }
            ]
            , onClickRow: function (row, $element) {
                $('.info').removeClass('info');//移除class
                $($element).addClass('info');//添加class
            }
        });
        $("#search").click(function () {
            $("#dataTable").bootstrapTable("refresh");
        });
        //新增按钮
        $("#Add_button").click(function () {
            addForm.resetForm();
            $('#productType_add_div .selectpicker').selectpicker('val', '');
            $('#productType_add_div .selectpicker').selectpicker('refresh');
            $("#addForm input").val("");
            $("#img_add").attr('src', '/images/no-logo.png');
            $("#JSESSIONID").val(JSON.parse(window.sessionStorage.getItem('JSESSIONID')));
            $("#addModel").modal("show");
        })

        //修改按钮
        $("#Update_button").click(function () {
            updateForm.resetForm();
            $("#updateForm input").val("");
            var rows = $('#dataTable').bootstrapTable("getSelections");
            if (rows == null || rows.length === 0) {
                $.dialog({
                    title: '提示!',
                    content: '请选择要操作的订单!',
                    backgroundDismiss: true
                });
                return;
            }
            $("#JSESSIONID_UPDATE").val(sessionStorage.getItem('JSESSIONID'));
            $("#updateModel").form("load", rows[0]);
            //默认选中
            $('#productType_update_div .selectpicker').selectpicker('val', rows[0].productType);
            $('#productType_update_div .selectpicker').selectpicker('refresh');
            $("#file2").attr('src', rows[0].picUrl);
            $("#updateModel").modal("show");
        })
        //新增保存
        $('#addSave').click(function () {
            var v = $("#addForm").valid();
            if (!v) {
                return;
            }

            $("#addForm").form("submit", {
                success: function (data) {
                    try {
                        data = eval('(' + data + ')');
                    } catch (e) {
                    }
                    if (data.success) {
                        $("#dataTable").bootstrapTable("refresh");
                        $("#addModel").modal("hide");
                        layer.msg('操作成功');
                    } else {
                        layer.msg(data.message)
                    }
                },
                error: function (e) {
                    layer.msg(e);
                }
            });
        });
        //修改保存
        $('#updateSave').click(function () {
            var v = $("#updateForm").valid();
            if (!v) {
                return;
            }

            $("#updateForm").form("submit", {
                success: function (data) {
                    try {
                        data = eval('(' + data + ')');
                    } catch (e) {
                    }
                    if (data.success) {
                        $("#dataTable").bootstrapTable("refresh");
                        $("#updateModel").modal("hide");
                        layer.msg('操作成功');
                    } else {
                        layer.msg(data.message);
                    }
                },
                error: function () {
                    layer.msg(e);
                }
            });
        });
        //删除按钮
        $('#Delete_button').click(function (e) {
            var rows = $('#dataTable').bootstrapTable("getSelections");
            if (rows === null || rows.length === 0) {
                layer.msg("请选择要操作的数据");
                return;
            }
            if (rows.length > 1) {
                layer.msg("仅允许单笔操作，请确认！");
                return;
            }
            $.confirm({
                title: '确认!',
                content: '确定要删除吗？',
                confirm: function () {
                    $.post("/aliProgramXianyuProduct/deleteAliProgramXianyuProduct", {id: rows[0].id}, function (data) {
                        if (data.success) {
                            $("#dataTable").bootstrapTable("refresh");
                            layer.msg(data.message);
                        } else {
                            layer.msg(data.message);
                        }
                    }).error(function (e) {
                        layer.msg(e);
                    });
                }
            })
        });

        //备注保存
        $("#toSaveRemark").click(function () {
            var remark = $('#remark').val();
            if (remark == null || remark == "") {
                layer.msg("备注不能为空!");
                return;
            }
            if (remark.length > 250) {
                layer.msg("输入备注超长!");
                return;
            }
            $.confirm({
                title: '确认!',
                content: '确定要' + productStatus($("#aliProductStatus").val()) + '吗？',
                confirm: function () {
                    $.ajax({
                        type: "POST",
                        url: "/aliProgramXianyuProduct/updateProductStatus",
                        data: {
                            ids: $("#ids").val(),
                            aliProductStatus: $("#aliProductStatus").val(),
                            remark: $("#remark").val()
                        },
                        dataType: "json",
                        success: function (data) {
                            if (data.success) {
                                $("#dataTable").bootstrapTable("refresh");
                                layer.msg("操作成功");
                                $("#productModal").modal("hide");
                            } else {
                                layer.msg(data.message)
                            }
                        },
                    });
                }
            });
        });

        function queryParams(params) {
            return {
                page: params.pageNumber,
                size: params.pageSize,
                productType: $.trim($("#productType").val()),
                spuName: $.trim($("#spuName_query").val()),
                spuId: $.trim($("#spuId_query").val()),
                submitType: $("#submitType_query").val(),
                aliProductStatus: $("#aliProductStatus_query").val(),
                spuStatus: $("#spuStatus_query").val()
            };
        }
    })

    //图片放大显示
    function watchDetailById(obj, id) {
        var src = $(obj).attr("src");
        var orderId = $(obj).attr("data-field");
        $.dialog({
            title: '图片详情',
            content: '<input hidden value="' + id
                + '" id="ticketFlowNoForSan"/><br/><img src="' + src + '"/>',
        });
    }

    function toChoseFile(obj) {
        $("#img_add").attr('src', '/images/no-logo.png');
        $(obj).prev("input[type=file]").click();
    }

    function preview(file) {
        var reader = new FileReader();
        reader.onload = function (evt) {
            $(file).next("img").attr("src", evt.target.result);
        }
        reader.readAsDataURL(file.files[0]);
    }

    function toChoseFileForUpdate(obj) {
        $(obj).prev("input[type=file]").click();
    }

    //产品上下架弹窗
    function updateStatus(v) {
        var rows = $('#dataTable').bootstrapTable("getSelections");
        if (rows == null || rows.length === 0) {
            layer.msg("请选择要操作的产品");
            return;
        }
        productIds = [];
        for (var i = 0; i < rows.length; i++) {
            productIds.push(rows[i].id);
        }
        var title = "";
        if (v == 'ON') {
            title = "上架"
        }
        if (v == 'OFF') {
            title = "下架"
        }

        $("#ids").val(productIds);
        $("#aliProductStatusStr").val(title);
        $("#aliProductStatus").val(v);
        $("#remark").val("");
        $("#productModal").modal("show");
    }
</script>
</body>
</html>
